﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZMenFM.Network.Album;
using System.Threading;
using ZMenFM.BASS;
using HCHANNEL = System.Int32;
using HSTREAM = System.Int32;
using ZMenFM.Utils.UI.Aero;
using ZMenFM.Network.Song;

namespace ZMenFM.Ramification.nBassTest
{
    public partial class Listner : Form
    {
        AlbumItem album;        //专辑
        HSTREAM stream = 0;     //流
        HCHANNEL channel = 0;   
        int cur = -1;           //下标

        LrcForm lrcform = null;

        public Listner()
        {
            InitializeComponent();
            lrcform = LrcForm.GetInstance("", 0);
            try
            {
                //播放器初始化
                Player.InitEnvi(this.Handle);
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void Listner_Load(object sender, EventArgs e)
        {
            //获取专辑资料

        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = listBox1.SelectedIndex;

            if (-1 == idx) return;

            //暂停与播放
            if (cur == idx)
            {
                if (channel != 0)
                {
                    if (Player.Channel_IsPlaying(channel))
                    {
                        Player.Channel_Pause(channel);
                        lrcform.GetTimer().Enabled = false;
                    }
                    else Player.Channel_Resume(channel);
                    lrcform.GetTimer().Enabled = true;
                }
                return;
            }

            cur = idx;


            if (album[idx].GetLoadStatus() == Network.Song.SongLoadStatus.SLS_NOT_START)
            {
                album[idx].Load();//加载歌曲
            }

            //如果正在加载，则等待，直到加载完
            while (album[idx].GetLoadStatus() == Network.Song.SongLoadStatus.SLS_LOADING) Thread.Sleep(10);

            
            //加载失败
            if (album[idx].GetLoadStatus() == Network.Song.SongLoadStatus.SLS_LOAD_FAILED)
            {
                MessageBox.Show("歌曲获取失败...");
                return;
            }
            //没有这首歌曲
            if (album[idx].GetFirstItem() == null)
            {
                MessageBox.Show("曲库中无此歌曲信息...");
                return;
            }
            Player.Channel_StopAll();
            //获取歌曲的流
            stream = Player.Stream_LoadFromUrl(album[idx].GetFirstItem().SongUrl);
            //播放
            channel = Player.Stream_Play(stream);
            lrcform.GetTimer().Enabled = true;

            ////////显示歌词///////
            LrcData lrcdata = new LrcData(album[idx].GetFirstItem().LrcUrl);
            while (lrcdata.GetStatus() == LrcLoadStatus.LLS_LOADING)
            {
                Thread.Sleep(10);
            }
            string lrc = lrcdata.GetLrcString();
            if (lrc == "")
            {
                lrc = "歌词加载失败";
            }
            lrcform = LrcForm.GetInstance(lrc,channel);
            lrcform.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            album = new AlbumItem(textBox1.Text);
            while (album.GetLoadStatus() == AlbumLoadStatus.ALS_LOADING) Thread.Sleep(10);

            if (album.GetLoadStatus() == AlbumLoadStatus.ALS_LOAD_FAILED)
            {
                MessageBox.Show("地址有误。");
                return;
            }
            Text = String.Format("{0} :: {1}", album.GetAlbumName(), album.GetAlbumUser());
            pictureBox1.ImageLocation = album.GetAlbumCover();
            cur = -1;
            listBox1.Items.Clear();
            for (int i = 0; i < album.Count; i++)
            {
                listBox1.Items.Add(String.Format("{0} - {1} - {2}", album.GetSongInfoList()[i].Name, album.GetSongInfoList()[i].Artist, album.GetSongInfoList()[i].Extra));
            }
        }
    }
}
